package com.demo.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.beans.Bean;
import com.demo.beans.Filename;
import com.demo.mapper.demo.FilenameMapper;
import com.demo.utils.DateConvert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class FilenameService {
    @Autowired
    private FilenameMapper filenameMapper;

    public Filename findByName(String fileName){
        Filename filename = this.filenameMapper.findByName(fileName);
        return filename;
    }
    public String saveFilename(String fileName){
        String fileNameId = "";
        Bean bean = new Bean();
        Filename filename = new Filename();
        filename.setFileName(fileName);
        String id = UUID.randomUUID().toString();
        id = id.substring(id.length()-17,id.length());
        filename.setId(id);
        long time = System.currentTimeMillis();
        filename.setCreateTime(DateConvert.millisecondToDate(time));
        int result = this.filenameMapper.insert(filename);
        if (result == 1){
            fileNameId = id;
            return fileNameId;
        }
        return null;
    }
    public boolean rollbackFilename(String filenameId){
        int i = this.filenameMapper.deleteById(filenameId);
        if (i > 0){
            return true;
        }
        return false;
    }
    public boolean deleteDatabase(){
        QueryWrapper<Filename> wrapper = new QueryWrapper<>();
        this.filenameMapper.delete(wrapper);
        Integer integer = this.filenameMapper.selectCount(wrapper);
        if (integer == 0){
            return true;
        }
        return false;
    }

}
